module.exports = function($scope, $state, $stateParams, $modalInstance, item, $resource,$modal,FileUploader,toaster){  
    console.log(item);
    if(item !== undefined){
        var id = item.id;
        console.log(id);
        // $scope.info = item;
        // $scope.info.market_price = $scope.info.market_price / 100;
        
    }
    
    $scope.cancel = function () {
        $modalInstance.close();
    };
    
    var uploader = $scope.uploader = new FileUploader({
        url: 'http://cl.juyouhx.com/oss.php/oss/webuploader1?topdir=aa&selfdir=bb'
    });

    uploader.filters.push({
        name: 'imageFilter',
        fn: function(item /*{File|FileLikeObject}*/, options) {
            var type = '|' + item.type.slice(item.type.lastIndexOf('/') + 1) + '|';
            return '|jpg|png|jpeg|bmp|gif|'.indexOf(type) !== -1;
        }
    });
    
    
    uploader.onSuccessItem = function(fileItem, response, status, headers) {
        $scope.info.picture = response.savename;
    };

     var uploader1 = $scope.uploader1 = new FileUploader({
        url: 'http://cl.juyouhx.com/oss.php/oss/webuploader1?topdir=aa&selfdir=bb'
    });

    uploader1.filters.push({
        name: 'imageFilter',
        fn: function(item /*{File|FileLikeObject}*/, options) {
            var type = '|' + item.type.slice(item.type.lastIndexOf('/') + 1) + '|';
            return '|jpg|png|jpeg|bmp|gif|'.indexOf(type) !== -1;
        }
    });
    
    
    uploader1.onSuccessItem = function(fileItem, response, status, headers) {
        $scope.info.photos = response.savename;
    };

    $scope.searchform = {
        'selected' :{
            'name' : ''
        }
    };

    // 获取销售品列表和商品信息
    $scope.getsalelist = function() {///api/as/tc/sale/alllist
        $resource('api/as/tc/sale/alllist', {}, {}).
        save($scope.searchform,function(res) {
            if (res.errcode!=0) {
                toaster.error({title: "", body:res.errmsg});
                return;
            }
            console.log(res);
            $scope.datas = res.data;
            var array = res.data;
             if (id) {
                $resource('/api/as/mc/merintegralmal/getInfoBySaleCode', {}, {}).                 
                save({'id' : id}, function(res){
                    if (res.errcode !== 0) {
                        toaster.error({title: "", body:res.errmsg});
                        return;
                    }
                    console.log(res);
                    $scope.info = res.data;
                    $scope.info.market_price = $scope.info.market_price/100;
                    $scope.info.integra_num = parseInt($scope.info.integra_num);
                    for (var i = 0; i < array.length ; i++) {
                        var codeStr = array[i].code;
                        //console.log(codeStr);
                        if (res.data.sale_code==codeStr) {
                            $scope.searchform.selected.name = array[i].name;
                            return;
                        }
                    }

                });
            } 


        });
        
    
    };

    $scope.getsalelist();


	$scope.savegoods = function() {
        if(id){
            $scope.info.sale_code = $scope.searchform.selected.code;
            $scope.info.market_price = $scope.info.market_price*100;
            $resource('/api/as/mc/merintegralmal/updateMal', {}, {}).
            save($scope.info, function(res){
                console.log($scope.info);
                if (res.errcode !== 0) {
                toaster.error({title: "", body:res.errmsg});
                return;
                }
                console.log(res);
                toaster.success({title: "", body:"修改成功"});                
                $modalInstance.close();
            });

        } else {   
            $scope.info.sale_code = $scope.searchform.selected.code;
            $scope.info.market_price = $scope.info.market_price*100; 
            $resource('/api/as/mc/merintegralmal/save', {}, {}).                 
            save($scope.info, function(res){
                console.log($scope.info);
                if (res.errcode !== 0) {
                toaster.error({title: "", body:res.errmsg});
                return;
                }
                console.log(res);
                toaster.success({title: "", body:"添加成功!"});                
                $modalInstance.close();
            });
        }
        
    };

};